package src.com.qf.controller;

import src.com.qf.entity.Address;
import src.com.qf.entity.User;
import src.com.qf.service.AddressService;
import src.com.qf.service.impl.AddressServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/address")
public class AddressController extends BaseController {
    private AddressService service = new AddressServiceImpl();

    /**
     * 查询当前用户下的地址列表并转发到self_info.jsp
     * @param request
     * @param response
     * @return
     */
    public String getAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        List<Address> addressList = service.getAddressList(user.getId());
        request.setAttribute("addList",addressList);
        return "forward:self_info.jsp";
    }

    /**
     * 设置默认的方法
     * @param request
     * @param response
     * @return
     */
    public String setDefault(HttpServletRequest request, HttpServletResponse response){
        String strId = request.getParameter("id");
        User user = (User) request.getSession().getAttribute("loginUser");

        int result = service.setDefault(Integer.parseInt(strId),user.getId());

        if (result == 0 ){
            request.setAttribute("msg","设置默认失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:address?action=getAddress";
        }

    }

    /**
     * 添加地址的方法
     * @param request
     * @param response
     * @return
     */
    public String addAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        String strName = request.getParameter("name");
        String strPhone = request.getParameter("phone");
        String strDetail = request.getParameter("detail");

        System.out.println("++++++++++++++++++++++++++++++++"+strDetail);
        int result = service.addAddress(strName,strPhone,strDetail,user.getId());

        if (result == 0){
            request.setAttribute("msg","添加地址失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:address?action=getAddress";
        }
    }

    /**
     * 删除地址的方法
     * @param request
     * @param response
     * @return
     */
    public String deleteAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        String strId = request.getParameter("id");

        int result = service.deleteAddress(strId,user.getId());

        if (result == 0){
            request.setAttribute("msg","数据删除失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:address?action=getAddress";
        }

    }

    /**
     * 修改地址的方法
     * @param request
     * @param response
     * @return
     */
    public String updateAddress(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        String strId = request.getParameter("id");
        String strName = request.getParameter("name");
        String strPhone = request.getParameter("phone");
        String strDetail = request.getParameter("detail");

        int result = service.updateAddress(strName,strPhone,strDetail,Integer.parseInt(strId),user.getId());
        if (result == 0){
            request.setAttribute("msg","数据修改失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:address?action=getAddress";
        }
    }
}
